/**
 * 部门js
 */
var userInfoDetail = {
	curPage : 1,
	pageSize : 10,
	dataCache : null,

	startEvent : function() {
		// 按回车键登录事件
		document.onkeydown = function(e) {
			var ev = document.all ? window.event : e;
			if (ev.keyCode == 13) {
				userInfoDetail.curPage = 1;
				userInfoDetail.setPageChange(null);
				userInfoDetail.search();
			}
		};
		
		jsUtils.controlFunctionBtn();
	},

	getChannelResource : function() {
		var url = 'mgr/userChannelCtrl/list';
		var params = {
			'curPage' : 0,
			'pageSize' : 9999
		};
		var cb = function(data) {
			if (data.resultCode == 200) {
				var option = '<option value="">请选择</option>';
				for (var i = 0; i < data.result.length; i++) {
					var singleObj = data.result[i];
					option = option + '<option value="' + singleObj.channelId + '">'
						+ singleObj.name + '</option>';
				}
				$('select[name="channel"]').append(option);
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');

	},

	bindEvent : function() {
		// 查询		
		$('#searchData').click(function() {
			userInfoDetail.curPage = 1;
			userInfoDetail.setPageChange(null);
			userInfoDetail.search();
		});		
		
//		$('.lookData').click(function(){
//			var thisObj=$(this);
//			var id=thisObj.parent().parent().find('input[name="id"]').eq(0).val();
//			art.dialog.data('lokId', id);
//			///hk-portal/src/main/webapp/mgr/statistics/userInfoDetail/userCardCurrent.jsp
//			var path=APP_ADDRESS+'mgr/statistics/userInfoDetail/userCardCurrent.jsp';
//			art.dialog.open(path, {
//			    title: '查看绑卡信息',
//			    width: 500,
//			    height:580,
//			    cancel: true
//			});
//		});
		
		$('#historyData').click(function() {
			userInfoDetail.curPage = 1;
			userInfoDetail.setPageChange(null);
			userInfoDetail.searchHistory();
		});

		 
		
		$('#excel').on('click', function() { 
			var url = $("#contextPath").val()+'mgr/userCardDetailCtr/excel';
//			if (typeof (pageIdx) != "undefined") {
//				userInfoDetail.curPage = pageIdx;
//			}

			var userNameOrPhone = $.trim($('#searchModForm input[name="userNameOrPhone"]').val());
			var finRealName = $.trim($('#searchModForm input[name="finRealName"]').val());
			var channel = $.trim($('#channel option:selected').val());

			 
			
			location.href = url+"?userNameOrPhone=" + userNameOrPhone + "&finRealName=" + finRealName   
				+ "&channel=" + channel;   
		});

		$('#syncBindCardAll').on('click',function () {
			var url = 'mgr/userCardDetailCtr/syncBindCardAll?actUserId='+userId;
            var cb = function(data) {
            	console.log(data)
                layer.msg(data.msg);
            };
            sendApiNoLoad(url, '', cb, 'GET');
        });
	},

	// 设置分页
	setPageChange : function(delCurPage) {
		var url = 'mgr/userCardDetailCtr/count';
	
		if (typeof (pageIdx) != "undefined") {
			userInfoDetail.curPage = pageIdx;
		}

	 
		var userNameOrPhone = $.trim($('#searchModForm input[name="userNameOrPhone"]').val());
		var finRealName = $.trim($('#searchModForm input[name="finRealName"]').val());
		var channel = $.trim($('#channel option:selected').val());
		
		 
		
		var params = {
			'userNameOrPhone' : userNameOrPhone,
			'finRealName' : finRealName,
			'channel' : channel,
			'curPage' : 1,
			'pageSize' : 9999999
		};

		var cb = function(data) {
			var totalCount = data;
			var pageTotal = (totalCount / userInfoDetail.pageSize > 0) ? Math
					.ceil(totalCount / userInfoDetail.pageSize) : 1;
			pageHelper.init(pageTotal, totalCount, userInfoDetail.search,
					delCurPage); // 翻页事件
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	// 查询交易记录
	search : function(pageIdx) {
		var url = 'mgr/userCardDetailCtr/list';
		if (typeof (pageIdx) != "undefined") {
			userInfoDetail.curPage = pageIdx;
		}

	 
		var userNameOrPhone = $.trim($('#searchModForm input[name="userNameOrPhone"]').val());
		var finRealName = $.trim($('#searchModForm input[name="finRealName"]').val());
		var channel = $('#channel option:selected') .val();
		
		 
	 
		var params = {
				'userNameOrPhone' : userNameOrPhone,
				'finRealName' : finRealName,
				'channel' : channel,
				'curPage' : userInfoDetail.curPage,
				'pageSize' : userInfoDetail.pageSize
		};
		
		var cb = function(data) {
			$('#tableList tbody').empty();
			var tr = '';
			if (data.result != null && data.result.length > 0) {
				for (var i = 0; i < data.result.length; i++) {
					var singleObj = data.result[i];
					console.dir(singleObj);
					tr += '<tr><td><input type="hidden" name="id" value="'
							+ singleObj.userId
							+ '"/>'
							+ (i + 1)
							+ '</td><td>'							
							+ singleObj.userPhone
							+ '</td><td>'							
							+ singleObj.userRealName
							+ '</td><td>'
							+ singleObj.finRealName
							+ '</td><td>'
							+ singleObj.finPhone
							+ '</td><td>'
							+ singleObj.userOrgName
							+ '</td><td>'
							+ singleObj.paymentNo
							+ '</td><td>'
							+ singleObj.fyCard
							+ '</td><td>'
							+ singleObj.hcCard
							+ '</td><td>'
							+ singleObj.ymCard
							+ '</td><td>'
							+'<a type="button" name="userCardDetail_99"'+' id='+singleObj.paymentId+' class="btn btn-primary btn-xs lookData" onclick="lookData(\''+singleObj.userPhone+'\''+","
							+'\''+singleObj.userRealName+'\''+","+'\''+ singleObj.finRealName+'\''+","+'\''+singleObj.userOrgName +'\''+","+'\''+ singleObj.paymentNo+'\''+","+'\''+singleObj.finPhone+'\''+","+'\''+singleObj.paymentId+'\''+","+'\''+singleObj.supportCompany+'\''+","+'\''+singleObj.bankName+'\');" style="margin-left: 5px; margin-top: 10px; display:none;">'
							+'<i class="fa fa-reorder"></i> 查看 </a>'
							+'<a type="button" name="userCardDetail_99"'+' id='+singleObj.paymentId+' class="btn btn-primary btn-xs historyData" onclick="historyData(\''+singleObj.paymentId+'\');" style="margin-left: 5px; margin-top: 10px; display:none;">'
							+'<i class="fa fa-reorder"></i> 解绑/换卡信息</a>'							
							+'</td></tr>';
				}
			} else {
				tr = '<tr><td colspan="6" style="text-align: center;color: red;">暂无数据</td></tr>';
			}
			$('#tableList tbody').html(tr);
			userInfoDetail.bindEventOtherBtn();
			jsUtils.controlFunctionBtn();
		};

		sendApiNoLoad(url, params, cb, 'POST');
	},
	
	bindEventOtherBtn : function() {
		
	},

	init : function() {

		userInfoDetail.setPageChange(null);
		userInfoDetail.search();
		userInfoDetail.startEvent();
		userInfoDetail.bindEvent();
		userInfoDetail.getChannelResource();
		jsUtils.controlFunctionBtn();
		
	}
};

$(document).ready(function() {
	userInfoDetail.init();
});

//提供给外部刷新调用
function refashData() {
	userInfoDetail.setPageChange(null);
	userInfoDetail.search();
}

function lookData(userPhone,userRealName,finRealName,userOrgName,paymentNo,finPhone,paymentId,supportCompany,bankName){
//	var thisObj=$(this);
//	var id=thisObj.parent().parent().find('input[name="id"]').eq(0).val();
	if(paymentId=="undefined"||paymentId=="null"){
		alert("没有绑卡信息！");
		return;
	}
	art.dialog.data('payment', userPhone+"|"+userRealName+"|"+finRealName+"|"+userOrgName+"|"+paymentNo+"|"+finPhone+"|"+paymentId+"|"+supportCompany+"|"+bankName);
						//mgr/statistics/userInfoDetail/userCardCurrent.jsp
	var path=APP_ADDRESS+'mgr/statistics/userCardDetail/userCardCurrent.jsp';
	art.dialog.open(path, {
	    title: '查看绑卡信息',
	    width: 700,
	    height:300,
	    cancel: true
	});
}

function historyData(paymentId){
	if(paymentId=="undefined"){
		alert("没有绑卡信息！");
		return;
	}
	art.dialog.data('paymentId', paymentId);
						//mgr/statistics/userInfoDetail/userCardCurrent.jsp
	var path=APP_ADDRESS+'mgr/statistics/userCardDetail/userCardHistory.jsp';
	art.dialog.open(path, {
	    title: '解绑/换卡信息',
	    width: 1200,
	    height:300,
	    cancel: true
	});
}
